//
//  SetLocalWordsController.m
//  万物记
//
//  Created by Ajen on 2019/6/20.
//  Copyright © 2019 Ajen. All rights reserved.
//

#import "SetLocalWordsController.h"
#import "NewChangeNicknameAlertView.h"
@interface SetLocalWordsController ()<UITableViewDelegate,UITableViewDataSource>{
    UIBarButtonItem * rightItem;
    UIBarButtonItem * rightItem2;
    NSMutableArray * muArray;
}
@property(nonatomic,strong)UITableView * tableView;

@end

@implementation SetLocalWordsController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithAjenHex:0x7a7374 alpha:1];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    UILabel * titleLabel = [UILabel new];
    titleLabel.aj_Text(@"词条管理").aj_textColorHexStr(@"#2d2e36").aj_addToView(self.view);
    titleLabel.font = SongKeFont(23);
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20);
        make.top.equalTo(self.view).offset(STATUS_BAR_HEIGHT);
        make.height.equalTo(@44);
    }];
    self.navigationItem.titleView = titleLabel;
    
    UILabel * topLabel = [UILabel new];
    topLabel.aj_Text(@"记住你的Anything~").aj_textColorHexStr(@"#ffffff").aj_addToView(self.view);
    topLabel.font = SongKeFont(19);
    [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(20);
        make.centerX.equalTo(self.view);
    }];
    
    UILabel * bottomLabel = [UILabel new];
    bottomLabel.aj_Text(@"万物记").aj_textColor([UIColor colorWithAjenHex:0xffffff alpha:0.3]).aj_addToView(self.view);
    bottomLabel.font = SongKeFont(60);
    [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-40);
        make.centerX.equalTo(self.view);
    }];
    
    
    
    
    
    
    //------------------------------------------------------------------
    
    
    
    
    
    rightItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemDown)];
    rightItem.tag = 200;
    rightItem.tintColor = [UIColor colorWithAjenHexString:@"#7a7374"];
    
    rightItem2 = [[UIBarButtonItem alloc] initWithTitle:@"新增" style:UIBarButtonItemStylePlain target:self action:@selector(addNewWordDown)];
    rightItem2.tintColor = [UIColor colorWithAjenHexString:@"#7a7374"];
    self.navigationItem.rightBarButtonItems= @[rightItem,rightItem2];
    
    muArray = [NSMutableArray arrayWithCapacity:0];
    if([[NSUserDefaults standardUserDefaults] objectForKey:@"LOCAL_WORDS"]){
        if([[[NSUserDefaults standardUserDefaults] objectForKey:@"LOCAL_WORDS"] count]){
            [muArray addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"LOCAL_WORDS"]];
        }
    }
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}


-(void)rightItemDown{
    if(rightItem.tag == 200){
        rightItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemDown)];
        rightItem.tag = 100;
        self.navigationItem.rightBarButtonItems = @[rightItem];
        self.tableView.editing = YES;
    }else{
        rightItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemDown)];
        rightItem.tag = 200;
        self.navigationItem.rightBarButtonItems= @[rightItem,rightItem2];
        self.tableView.editing = NO;
    }
    rightItem.tintColor = [UIColor colorWithAjenHexString:@"#7a7374"];
}

-(void)addNewWordDown{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"新增词条" message:@"建议词条长度在50字以内" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UITextField *textField = alertController.textFields.firstObject;
        NSArray * localTitleArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"LOCAL_WORDS"];
        if([localTitleArray containsObject:textField.text]){
            [self showMsg:@"该词条已存在,请更换词条内容"];
        }else{
            [muArray addObject:textField.text];
            [[NSUserDefaults standardUserDefaults] setObject:muArray forKey:@"LOCAL_WORDS"];
            [self.tableView reloadData];
        }
    }]];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请输入快捷词条";
    }];
    [self presentViewController:alertController animated:true completion:nil];
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return muArray.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier =@"UITableViewCell";
    UITableViewCell *cell = [self.tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UIAccessibilityTraitNone;
    }
    cell.textLabel.text = [NSString stringWithFormat:@"(%ld) : %@",indexPath.row + 1,muArray[indexPath.row]];
    cell.textLabel.textColor = [UIColor colorWithAjenHex:0x2d2e36 alpha:0.8];
    cell.textLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightThin];
    return cell;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if(rightItem.tag == 200){
        return NO;
    }else{
        return YES;
    }
}


-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    muArray = [[muArray AJJumpToIndex:sourceIndexPath.row with:destinationIndexPath.row] mutableCopy];
    [self.tableView reloadData];
    [[NSUserDefaults standardUserDefaults] setObject:muArray forKey:@"LOCAL_WORDS"];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [muArray removeObjectAtIndex:indexPath.row];
        [self.tableView reloadData];
        [[NSUserDefaults standardUserDefaults] setObject:muArray forKey:@"LOCAL_WORDS"];
    }
}

-(NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexpath {
    return @"删除该词条";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NewChangeNicknameAlertView * changeNickNameView = [NewChangeNicknameAlertView new];
    changeNickNameView.normalNickName = muArray[indexPath.row];
    [[UIApplication sharedApplication].keyWindow addSubview:changeNickNameView];
    [changeNickNameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo([UIApplication sharedApplication].keyWindow);
    }];
    [UIView animateWithDuration:0.3 animations:^{
        changeNickNameView.backgroundColor = [UIColor colorWithAjenHex:0x000000 alpha:0.5];
    }];
    
    changeNickNameView.commitBlock = ^(NSString * _Nonnull nickName) {
        muArray[indexPath.row] = nickName;
        [self.tableView reloadData];
        [[NSUserDefaults standardUserDefaults] setObject:muArray forKey:@"LOCAL_WORDS"];
    };
    changeNickNameView.alertStr = @"编辑词条内容(建议50字内)";
    
}

-(UITableView *)tableView{
    if(_tableView == nil){
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableView.delegate =self;
        _tableView.dataSource=self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorColor = [UIColor colorWithAjenHex:0x7a7374 alpha:0.2];
        _tableView.backgroundColor = [UIColor colorWithAjenHex:0xffffff alpha:0.9];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.scrollIndicatorInsets = _tableView.contentInset;
        }
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        UIView *tableViewHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, _tableView.bounds.size.width, CGFLOAT_MIN)];
        _tableView.tableHeaderView = tableViewHeaderView;
    }
    return _tableView;
}

@end
